//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    sig: true,
    recommList: [],
    book: [],
    power:[],//能量
    article:[], // 膳食
    userInfo:{},
    imgArr: []
  },

  onLoad: function () {
    wx.hideTabBar();
    app.util.dateNowadays((res)=>{
      if(res){
        wx.showTabBar();
      }
      this.setData({
        sig: !res
      })
    });
    app.UserInfo((res) => {
      this.setData({
        userInfo: res
      })
    });
    this.getRecommList();
    this.power();
    this.article();
    this.getBanner();
  },
  onShow: function () {
    // 获取预定缓存
    let book = wx.getStorageSync('book') || [];
    this.setData({
      book: book
    })
  },
  onHide: function () {
    wx.setStorage({
      key:"book",
      data: this.data.book
    });
  },
  onPullDownRefresh: function () {
    this.getRecommList();
    this.power();
    this.article();
    this.getBanner();
  },
  // 获取banner
  getBanner() {
    let url = 'mp/banner/list';
    app.ajax(url, 'GET').then(res => {
      console.log('banner数据', res);
      if (res.status === 10000) {
        this.setData({
          imgArr: res.data
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  //推荐预定
  getRecommList() {
    let url = 'mp/dishes/recommend';
    app.ajax(url, 'GET').then(res => {
      if (res.status === 10000) {
        this.setData({
          recommList: res.data
        })
      }
    }).catch(err => {
      console.log(err)
    })
  },
  //签到
  signIn() {
    let that = this
    app.ajax(app.Api.userCheckIn, 'POST', {}).then(res => {
        console.log(res)
        wx.showToast({
          title: '签到成功',
          success:function(){
            wx.navigateTo({
              url: 'pages/signIn/index',
            })
          }
        })
        //更新用户信息
        app.UserInfo();
        that.setData({
          sig: false
        })
      wx.showTabBar();
    }).catch(err => {
        console.log('报错了', err)
      })

  },
  // 自定义方法
  closeFun () {
    this.setData({sig:false})
    wx.showTabBar();
  },
  // 推荐预定点击
  addDish(e) {
    console.log('推荐预定')
    let item = e.currentTarget.dataset.item.dishes_list;
    let book = JSON.parse(JSON.stringify(this.data.book));
    // 寻找是否存在
    let index = book.findIndex(val => { return val.id === item.id });
    if (index === -1) {
      book.push(item)
    }
    this.setData({
      book: book
    })
  },
  //能量
  
  power(){
    let that = this, power = this.data.power;
    app.ajax('mp/power/list', 'GET', { limit: 10, offset:1}).then(res => {
          console.log(res)
        power = [...res.data.rows];
        this.setData({
          power
        })
    }).catch(err => {
      console.log('报错了', err)
    })
  },
  article() {
    let article = this.data.article;
    app.ajax('mp/article/list', 'GET', { limit: 20, offset: 1, article_column_id: '1' }).then(res => {
      if (res && res.data) {
        article = [...res.data];
        this.setData({
          article
        })
      }

    }).catch(err => {
      console.log('报错了', err)
    })
  },
  goUrl (e) {
    let url = e.currentTarget.dataset.url;
    // 如果type为1 那就是跳转到预约 先判断人员 在判断是否下过餐
    let type = e.currentTarget.dataset.type || 0;
    let time = app.util.formatDate(new Date(), 'h');
    let userInfo = this.data.userInfo;
    if(type == 1){
      // 非值班人员不能进入早餐晚餐
      if (userInfo.is_duty == 0){
        if (url == '/pages/reservation/index?type=1' || url == '/pages/reservation/index?type=3'){
          wx.showToast({
            title: '非值班人员无法预订早/晚餐',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
      }
      let day = app.util.formatDate(new Date(), 'YMD');
      let storage = wx.getStorageSync(day) || {};
      let goType = url.substr(url.length - 1, 1);
      if (storage[goType] == true){
        wx.showToast({
          title: '一个时间段只能预订一单哦',
          icon: 'none',
          duration: 2000
        })
        return false;
      }
      if (url == '/pages/reservation/index?type=1'){
        if (+time > 16) {
          wx.showToast({
            title: '早餐只能在下午四点前预订哦',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
      } else if (url == '/pages/reservation/index?type=2'){
        if (+time > 10) {
          wx.showToast({
            title: '中餐只能在上午十点前预订哦',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
      } else if (url == '/pages/reservation/index?type=3') {
        if (+time > 15) {
          wx.showToast({
            title: '晚餐只能在下午三点前预订哦',
            icon: 'none',
            duration: 2000
          })
          return false;
        }
      }
    }
    wx.navigateTo({
      url: url,
    })
  }
})
